package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.manage.domain.MaUser;
import com.zhentao.model.common.manage.dto.UserDto;
import com.zhentao.service.MaUserService;
import com.zhentao.mapper.MaUserMapper;
import com.zhentao.utils.common.AppJwtUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
* @author lenovo
* @description 针对表【ma_user】的数据库操作Service实现
* @createDate 2024-09-14 09:59:24
*/
@Service
public class MaUserServiceImpl extends ServiceImpl<MaUserMapper, MaUser>
    implements MaUserService{
    @Resource
    private MaUserMapper maUserMapper;

    @Override
    public ResponseResult login(UserDto userDto) {
        if(StringUtils.isEmpty(userDto)){
            return ResponseResult.errorResult(400,"参数不能为空");
        }
        MaUser maUser = maUserMapper.selectOne(Wrappers.<MaUser>lambdaQuery().eq(MaUser::getPhone, userDto.getPhone()));
        if(maUser==null){
            return ResponseResult.errorResult(400,"用户不存在");
        }
        String salt = maUser.getSalt();
        String password = userDto.getPassword();
        String s = DigestUtils.md5DigestAsHex((salt + password).getBytes());
        System.out.println(s);
        if(!s.equals(maUser.getPassword())){
            return ResponseResult.errorResult(400,"密码错误");
        }
        Map<String, Object> map = new HashMap<>();
        maUser.setPassword("");
        maUser.setSalt("");
        map.put("token", AppJwtUtil.getToken(maUser.getId().longValue()));
        map.put("user", maUser);
        return ResponseResult.okResult(map);
    }

    @Override
    public ResponseResult findAll(Integer id) {
        MaUser maUser = maUserMapper.selectOne(Wrappers.<MaUser>lambdaQuery().eq(MaUser::getId, id));
        return ResponseResult.okResult(maUser);
    }
}




